
import 'dart:convert';

import 'package:freemusic/model/AbstractSong.dart';
import 'package:freemusic/model/Lyric.dart';

class NeteaseSong extends AbstractSong {


  @override
  List<AbstractSong> fromRemoteJosn(Map json) {

    List<NeteaseSong> netEaseSongs = List();

    final map = json;
    Map result = map['result'];
    List songs = result['songs'];
    songs.forEach((item){
      NeteaseSong song = NeteaseSong();
      song.hash = "netease_"+ item['id'].toString();
      song.songName = item['name'];
      song.img = item['picId'];
      List artists = item['artists'];
      song.singerName = artists[0]['name'];
      song.timeLength = Duration(milliseconds: item['duration']);
      song.platform = SongPlatform.netease;
      song.type = "mp3";

      netEaseSongs.add(song);
    });

    return netEaseSongs;
  }

  @override
  Lyric stringToLyric() {

    Lyric lyric = Lyric();

    if (lyrics == null || lyrics.isEmpty ) {
      return lyric;
    }
    List<String> list = lyrics.split("\n");
    list.forEach((item) {
      if (!item.endsWith("]") && item.isNotEmpty) {
        lyric.slices.add(stringToLyricSlice(item));
      }
    });
    return lyric;
  }
  LyricSlice stringToLyricSlice(String line) {
    LyricSlice lyricSlice = LyricSlice();
    List<String> lines = line.split("]");
    String timeStr = lines[0];
    lyricSlice.slice = lines[1];
    int minute = int.parse(timeStr.substring(1, 3));
    int second = int.parse(timeStr.substring(4, 6));
    int millis = int.parse(timeStr.substring(7, 9));

    // lyricSlice.milliseconds = minute * 60 * 1000 + second * 1000 + millis;
    lyricSlice.milliseconds = Duration(minutes: minute, seconds: second, milliseconds: millis);
    return lyricSlice;
  }
  
}